<template>
  <div class="cart">
    <div class="cart-main">
      <div class="cart-img">
        <link
          rel="stylesheet"
          href="//at.alicdn.com/t/c/font_3585743_92x1ppg3xzq.css"
        />
        <span class="cart-img1"><i class="iconfont icon-youhuijuan"></i></span>
        <span class="cart-img2">购物车可领取优惠卷</span>
      </div>
      <div class="sum-cart">
        <span class="sum-cart1">领卷</span>
      </div>
    </div>
    <div class="cart-th">
      <div class="cart-th1">
        <input class="chooseAll" type="checkbox" v-model="allChecked" />
        <span>全选</span>
      </div>
      <div class="cart-th2">商品信息</div>
      <div class="cart-th3">单价</div>
      <div class="cart-th4">数量</div>
      <div class="cart-th5">小计</div>
      <div class="cart-th6">操作</div>
    </div>
    <div class="cart-mid">
      <div class="cart-smid">
        <span class="cart-smid1">全场换购</span>
        <span class="cart-smid2">已满足一件享低至四折超值换购</span>
        <span class="cart-smid3"
          ><a class="cart-ssmid3" href="">去换购</a></span
        >
        <span class="cart-smid4"
          ><a class="cart-ssmid4" href="">继续凑单>></a></span
        >
      </div>
    </div>
    <div class="cart-body">
      <div class="cart-now">
        <div class="cart-mid">
          <div class="cart-smid" id="cart-MID">
            <span class="cart-smid1">满额减</span>
            <span class="cart-smid2">再购10元立享满199元减20元</span>
            <span class="cart-smid4"
              ><a class="cart-ssmid4" href="">继续凑单>></a></span
            >
          </div>
        </div>
      </div>
      <div class="cart-body1">
        <ul class="cart-list" v-for="cart in cartList" :key="cart.id">
          <li class="cart-list-con1">
            <input
              type="checkbox"
              name="chk_list"
              :checked="cart.isChecked ? true : false"
              @change="setIsChecked(cart, 1 - cart.isChecked)"
            />
          </li>
          <li class="cart-list-con2">
            <img :src="cart.imgUrl" :alt="cart.skuName" />
            <div class="item-msg">{{ cart.skuName }}</div>
          </li>
          <li class="cart-list-con4">
            <span class="price">${{ cart.cartPrice }}</span>
          </li>
          <li class="cart-list-con5">
            <InputNumber :value="cart.skuNum" />
          </li>
          <li class="cart-list-con6">
            <span class="sum">${{ cart.cartPrice * cart.skuNum }}</span>
          </li>
          <li class="cart-list-con7">
            <span><a href="#none" class="sindelet">移入收藏夹</a></span>
            <br />
            <span><a href="#none" class="sindelet">删除</a></span>
            <br />
          </li>
        </ul>
      </div>
    </div>
    <div class="cart-tool">
      <div class="cart-tool1">
        <div class="select-all">
          <span>已选({{ checkedCartNum }})</span>
        </div>
        <div class="option">
          <a href="#none">批量删除</a>
          <a href="#none">清空失效商品</a>
        </div>
      </div>
      <div class="money-box">
        <div class="sumprice">
          <em>应付金额:</em>
          <i class="summoney">${{ totalPrice }} </i>
        </div>
        <div class="sumbtn">
          <router-link to="/trade" class="sum-btn">下单</router-link>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
export default {
  name: 'WaddShopCart',
}
</script>

<script lang="ts" setup>
import { ref, onMounted, computed } from 'vue'
import { getShopCartApi, getCartIsCheckedApi } from '@/api/shop/shopcart'
import { CartList } from '@/api/shop/interface/index'
import InputNumber from '@/components/InputNumber/index.vue'

const cartList = ref<CartList[]>([])

const getShopCartList = async () => {
  const result = await getShopCartApi()
  console.log(result)
  console.log(result.cartInfoList)

  cartList.value = result[0].cartInfoList
}
// 单选
const setIsChecked = async (cart, isChecked) => {
  cart.isChecked = isChecked
  await getCartIsCheckedApi(cart.skuId, isChecked)
}

//全选
const allChecked = computed({
  get: () => {
    return (cartList.value as any).every((cart) =>
      cart.isChecked ? true : false
    )
  },
  set: (newVal) => {
    const isChecked = newVal ? 1 : 0
    cartList.value.forEach((cart) => {
      if (cart.isChecked === isChecked) return
      setIsChecked(cart, isChecked)
    })
  },
})

//已选择商品数据
const checkedCartNum = computed(() => {
  return cartList.value.reduce((p, c) => {
    return p + c.isChecked
  }, 0)
})
//总价
const totalPrice = computed(() => {
  return cartList.value.reduce((p, c) => {
    return p + (c.isChecked ? c.cartPrice * c.skuNum : 0)
  }, 0)
})

onMounted(() => {
  getShopCartList()
})
</script>

<style scoped>
.cart {
  width: 1090px;
  margin: 0 auto;
  font: 12px/1.5 'Microsoft Yahei', '微软雅黑';
}
.cart-img1 i {
  margin: 9px 17px 0 12px;
  background-position: 0 -105px;
  height: 20px;
  width: 28px;
  color: orange;
  font-size: 30px;
}
.cart-img2 {
  color: #69c;
}
.cart-main {
  width: 1090px;
  height: 40px;
  margin-top: 10px;
  border: 1px solid #eaeaea;
  line-height: 38px;
}
.cart-img span {
  float: left;
}
.cart-img2 {
  line-height: 38px;
  font-size: 14px;
}
.sum-cart {
  float: right;
  padding: 0 26px;
  height: 38px;
  line-height: 38px;
  text-align: center;
  color: #fff;
  background-color: #e36844;
  cursor: pointer;
}
.cart-th {
  background: #f5f5f5;
  border: 1px solid #ddd;
  padding: 12px 0;
  overflow: hidden;
  font-size: 13px;
  margin-top: 10px;
  margin-bottom: 20px;
}
.cart-th div {
  float: left;
}
.cart-th1 {
  width: 13%;
  margin-left: 20px;
}
.cart-th1 span {
  margin-left: 14px;
}
.cart-th2 {
  width: 28%;
}
.cart-th3 {
  width: 17%;
}
.cart-th4 {
  width: 19%;
}
.cart-th5 {
  width: 10%;
}
.cart-th6 {
  width: 11%;
}
.cart-mid {
  border: 1px solid #ddd;
  background-color: #fffbf0;
  margin-bottom: 10px;
  padding: 0 30px 0 60px;
  height: 43px;
}
.cart-smid {
  line-height: 43px;
}
.cart-smid span {
  float: left;
}
.cart-smid1 {
  margin: 11px 10px 0 0;
  display: inline-block;
  height: 20px;
  line-height: 20px;
  color: #fff !important;
  padding: 0 5px;
  font-size: 12px;
  background-color: #e36844;
}
.cart-smid2 {
  margin-right: 20px;
  font-size: 14px !important;
}
.cart-ssmid3 {
  width: 66px;
  border: 1px solid #d4282d;
  color: #d4282d;
  margin-right: 20px;
  height: 24px;
  line-height: 24px;
  text-align: center;
  background-color: #fff;
  display: inline-block;
}
.cart-ssmid4 {
  font-size: 14px !important;
  color: #69c;
}
.cart-body {
  border: 1px solid #ddd;
  margin-bottom: 10px;
  background-color: #fffbf0;
}
.cart-now .cart-mid {
  border: none;
}
#cart-MID {
  height: 43px;
  border-bottom: 1px solid #ddd;
}
.cart-list {
  display: flex;
  align-items: center;
  padding: 20px 0;
  border-bottom: 1px solid #ddd;
  overflow: hidden;
}
.cart-list li {
  float: left;
}
.cart-list-con1 {
  text-align: center;
  width: 5%;
}
.cart-list-con2 {
  width: 37%;
}
.cart-list-con2 img {
  width: 82px;
  height: 82px;
  float: left;
}
.item-msg {
  float: left;
  width: 150px;
  margin: 0 10px;
  line-height: 18px;
  overflow: hidden;
}
.cart-list-con4 {
  width: 15%;
}
.cart-list-con4 .price {
  font-size: 14px;
}
.cart-list-con5 {
  width: 22%;
}
.cart-list-con5 .mins {
  border: 1px solid #ddd;
  border-right: 0;
  float: left;
  color: #666;
  width: 6px;
  text-align: center;
  padding: 8px;
  text-decoration: none;
}
.cart-list-con5 input {
  border: 1px solid #ddd;
  width: 40px;
  height: 31px;
  float: left;
  text-align: center;
  font-size: 14px;
  color: red;
}

.cart-list-con5 .plus {
  border: 1px solid #ddd;
  border-left: 0;
  float: left;
  color: #666;
  width: 6px;
  text-align: center;
  padding: 8px;
  text-decoration: none;
}

.cart-list-con6 {
  width: 11%;
}
.sum {
  font-size: 16px;
  color: #d4282d;
}

.cart-list-con7 {
  width: 10%;
}
.cart-list-con7 a {
  color: #666;
}
.cart-tool {
  font-size: 14px;
  position: relative;
  background-color: #f5f5f5;
  border: 1px solid #ddd;
  height: 70px;
  line-height: 70px;
}

.select-all {
  padding: 10px;
  overflow: hidden;
  float: left;
}
.select-all span {
  margin-left: 14px;
  vertical-align: middle;
  font-size: 14px;
  color: #69c;
}

.select-all input {
  vertical-align: middle;
}
.option {
  padding: 10px;
  overflow: hidden;
  float: left;
  display: flex;
  align-items: center;
}
.option a {
  float: left;
  margin-left: 30px;
  color: #666;
  font-size: 14px;
}
.money-box {
  float: right;
}

.money-box .sumprice {
  width: 200px;
  font-size: 22px;
  float: left;
  margin-top: 12px;
}
.sumprice em {
  font-size: 14px;
  display: inline-block;
  margin-right: 20px;
}
.summoney {
  color: #c81623;
  font-size: 16px;
}
.sumbtn {
  float: right;
}
.sumbtn a {
  display: block;
  position: relative;
  width: 140px;
  height: 70px;
  line-height: 70px;
  color: #fff;
  text-align: center;
  font-size: 18px;
  font-family: 'Microsoft YaHei';
  border: 1px solid #b4a078;
  background-color: #b4a078;
  overflow: hidden;
  text-decoration: none;
}
.sum-btn:hover {
  color: white !important;
}
</style>
